﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Collections;

namespace FuturamaPDA {
	class Connector {
		private HttpWebRequest request;
		private HttpWebResponse response;
		private ArrayList queryData;
		private readonly string url = "http://data.manuras.com";
		private string parameters;
		private StreamWriter sw;
		private StreamReader sr;
		 
		public Connector() {
			this.request = (HttpWebRequest)WebRequest.Create(this.url);
			this.request.Method = "POST";
			this.queryData = new ArrayList();
		}
		 
		public void Add(string key, string value) {
			this.queryData.Add(String.Format("{0}={1}",key,value));
		}

		public string GetResponse() {
			string finalResponse = "";
			try {
				// Set the encoding type
				this.request.ContentType="application/x-www-form-urlencoded";
				 
				// Build a string containing all the parameters
				this.parameters = String.Join("&",(String[]) this.queryData.ToArray(typeof(string)));
				this.request.ContentLength = this.parameters.Length;
				 
				// We write the parameters into the request
				this.sw = new StreamWriter(this.request.GetRequestStream());
				this.sw.Write(this.parameters);
				this.sw.Close();
				 
				// Execute the query
				this.response = (HttpWebResponse)this.request.GetResponse();
				this.sr = new StreamReader(this.response.GetResponseStream());
				finalResponse = this.sr.ReadToEnd();
				this.sr.Close();
			} catch (WebException) {
				finalResponse = "<?xml version='1.0' encoding='ASCII'?><error></error>";
			}
			
			return finalResponse;
		}
	}
}